home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / MacApp / MacApp CD Release / MacApp 2.0.1 (Many Libraries) / Libraries / UFailure.inc1.p < prev    next >
Encoding:
Text File  |  1990-10-25  |  7.4 KB  |  343 lines  |  [TEXT/MPS ]

  1. {$P}
  2. {[a-,body+,h-,o=100,r+,rec+,t=4,u+,#+,j=20/57/1$,n-]}
  3. {UFailure.inc1.p}
  4. {Copyright © 1985-1990 Apple Computer, Inc.  All rights reserved.}
  5.  
  6. {$IFC NOT qDebugTheDebugger}
  7. {$W+}
  8. {$R-}
  9. {$Init-}
  10. {$OV-}
  11. {$ENDC}
  12.  
  13. VAR
  14.     pWho:                MAName;                         { used serially. Avoids putting it on stack
  15.                                                          }
  16.     {$Push} {$J+}
  17.     {$IFC qDebug}
  18.     GINTENSEDEBUGGING:    Boolean;                        { Since we can't USE UMacApp (2.0) }
  19.     {$EndC}
  20.     {$Pop}
  21.  
  22. PROCEDURE ApplicationBeep;
  23.     EXTERNAL;
  24.  
  25. PROCEDURE CatchFailures(VAR fi: FailInfo;
  26.                         PROCEDURE Handler(e: INTEGER;
  27.                                           m: LONGINT));
  28.     EXTERNAL;
  29.  
  30. PROCEDURE DoFailure(pf: FailInfoPtr);
  31.     EXTERNAL;
  32.  
  33. {--------------------------------------------------------------------------------------------------}
  34. {$S MAFailureRes}
  35.  
  36. PROCEDURE Assertion(condition: Boolean;
  37.                     description: StringPtr);
  38.  
  39.     BEGIN
  40.     IF NOT condition THEN
  41.         BEGIN
  42.         {$IFC qDebug}
  43.         GetCallersMethodName(pWho);
  44.         IF CanReadLn THEN
  45.             BEGIN
  46.             WriteLn(Concat('Assertion failed in ', pWho, ': ', description^));
  47.             EnterMacAppDebugger;
  48.             IF ReadYesNo(Concat('Do you want to signal failure?')) THEN
  49.                 Failure(minErr, 0);                     { ??? silent failure, but someday 0
  50.                                                          messages need to be non-silent }
  51.             END
  52.         ELSE
  53.             BEGIN
  54.             DebugStr(Concat('Assertion failed in ', pWho, ': ', description^));
  55.             Failure(minErr, 0);                         { ??? silent failure, but someday 0
  56.                                                          messages need to be non-silent }
  57.             END;
  58.         {$ELSEC}
  59.         Failure(minErr, 0);                             { ??? silent failure, but someday 0
  60.                                                          messages need to be non-silent }
  61.         {$EndC}
  62.         END;
  63.     END;
  64.  
  65. {--------------------------------------------------------------------------------------------------}
  66. {$S MAFailureRes}
  67.  
  68. PROCEDURE EachFailureHandlerDo(PROCEDURE DoToHandler(fiPtr: FailInfoPtr));
  69.  
  70.     VAR
  71.         pf:                 FailInfoPtr;
  72.  
  73.     BEGIN
  74.     pf := gTopHandler;
  75.  
  76.     WHILE (pf <> NIL) DO
  77.         BEGIN
  78.         DoToHandler(pf);
  79.         pf := pf^.nextInfo;
  80.         END;
  81.     END;
  82.  
  83. {--------------------------------------------------------------------------------------------------}
  84. {$Push} {$IFC qTrace} {$D+} {$ENDC}                     { Call to %_BP could croak the MemError }
  85. {$S MAFailureRes}
  86.  
  87. PROCEDURE FailMemError;
  88.  
  89.     VAR
  90.         e:                    OSErr;
  91.  
  92.     BEGIN
  93.     e := MemError;
  94.  
  95.     {$IFC qDebug}
  96.     IF gAskFailure & (e = noErr) & CanReadLn THEN
  97.         BEGIN
  98.         GetCallersMethodName(pWho);
  99.         e := ReadInteger(Concat('FailMemError called by ', pWho, '.  Enter return error: '));
  100.         END;
  101.     {$ENDC}
  102.  
  103.     IF e <> noErr THEN
  104.         Failure(e, 0);
  105.     END;
  106. {$Pop}
  107.  
  108. {--------------------------------------------------------------------------------------------------}
  109. {$S MAFailureRes}
  110.  
  111. PROCEDURE FailNewMessage(error: INTEGER;
  112.                          oldMessage, newMessage: LONGINT);
  113.  
  114.     BEGIN
  115.     IF oldMessage = 0 THEN
  116.         oldMessage := newMessage;
  117.     Failure(error, oldMessage);
  118.     END;
  119.  
  120. {--------------------------------------------------------------------------------------------------}
  121. {$S MAFailureRes}
  122.  
  123. PROCEDURE FailNIL(p: UNIV Ptr);
  124.  
  125.     BEGIN
  126.     { no check for gAskFailure here, since we do this when objects are created. }
  127.     IF p = NIL THEN
  128.         Failure(memFullErr, 0);
  129.     END;
  130.  
  131. {--------------------------------------------------------------------------------------------------}
  132. {$S MAFailureRes}
  133.  
  134. PROCEDURE FailNILResource(r: UNIV Handle);
  135.  
  136.     VAR
  137.         e:                    OSErr;
  138.  
  139.     BEGIN
  140.     {$IFC qDebug}
  141.     IF gAskFailure & (r <> NIL) & CanReadLn THEN
  142.         BEGIN
  143.         GetCallersMethodName(pWho);
  144.         IF ReadYesNo(Concat('FailNilResource called by ', pWho, '.  Return NIL?: ')) THEN
  145.             r := NIL;
  146.         END;
  147.     {$ENDC}
  148.  
  149.     IF r = NIL THEN
  150.         BEGIN
  151.         e := ResError;
  152.         IF e = noErr THEN
  153.             e := resNotFound;
  154.         Failure(e, 0);
  155.         END;
  156.     END;
  157.  
  158. {--------------------------------------------------------------------------------------------------}
  159. {$S MAFailureRes}
  160.  
  161. PROCEDURE FailOSErr(error: INTEGER);
  162.  
  163.     BEGIN
  164.     {$IFC qDebug}
  165.     IF gAskFailure & (error = noErr) & CanReadLn THEN
  166.         BEGIN
  167.         GetCallersMethodName(pWho);
  168.         error := ReadInteger(Concat('FailOSErr called by ', pWho, '.  Enter return error: '));
  169.         END;
  170.     {$ENDC}
  171.  
  172.     IF error <> noErr THEN
  173.         Failure(error, 0);
  174.     END;
  175.  
  176. {--------------------------------------------------------------------------------------------------}
  177. {$Push} {$IFC qTrace} {$D+} {$ENDC}                     { Call to %_BP could croak the ResError }
  178. {$S MAFailureRes}
  179.  
  180. PROCEDURE FailResError;
  181.  
  182.     VAR
  183.         e:                    OSErr;
  184.  
  185.     BEGIN
  186.     e := ResError;
  187.  
  188.     {$IFC qDebug}
  189.     IF gAskFailure & (e = noErr) & CanReadLn THEN
  190.         BEGIN
  191.         GetCallersMethodName(pWho);
  192.         e := ReadInteger(Concat('FailResError called by ', pWho, '.  Enter return error: '));
  193.         END;
  194.     {$ENDC}
  195.  
  196.     IF e <> noErr THEN
  197.         Failure(e, 0);
  198.     END;
  199. {$Pop}
  200.  
  201. {--------------------------------------------------------------------------------------------------}
  202. {$S MAFailureRes}
  203.  
  204. PROCEDURE Failure(error: INTEGER;
  205.                   message: LONGINT);
  206.  
  207.     VAR
  208.         pf:                 FailInfoPtr;
  209.         pc:                 LONGINT;
  210.  
  211.     BEGIN
  212.     pf := gTopHandler;
  213.  
  214.     IF pf <> NIL THEN
  215.         BEGIN
  216.       {pop the stack first, because calling the handler is likely to
  217.        result in a call to Failure}
  218.         gTopHandler := pf^.nextInfo;
  219.  
  220.         {$IFC qDebug}
  221.         IF CanWriteLn & ((error <> 0) | GINTENSEDEBUGGING) THEN { only show 0 errors if _really_
  222.                                                                  looking }
  223.             BEGIN
  224.             GetCallersMethodName(pWho);
  225.             WriteLn('Failure signaled by: ', pWho);
  226.             pc := pf^.whoPC;
  227.             GetMethodName(LONGINT(@pc), pWho);
  228.  
  229.             WriteLn('Failure caught by: ', pWho);
  230.             WriteLn('        error: ', error: 1, ' message: ', message: 1, ' (', BSR(message, 16):
  231.                     1, '/', BAND(message, $0000FFFF): 1, ')');
  232.             END;
  233.         {$ENDC}
  234.  
  235.         pf^.error := error;
  236.         pf^.message := message;
  237.         DoFailure(pf);                                    {Go execute the failure handler}
  238.         END
  239.     ELSE
  240.         BEGIN
  241.         {$IFC qDebug}
  242.         ProgramBreak('Failure called, but no handler!');
  243.         {$ELSEC}
  244.         DebugStr('Failure called, but no handler!');
  245.         {$ENDC}
  246.         END;
  247.     END;
  248.  
  249. {--------------------------------------------------------------------------------------------------}
  250. {$Push} {$IFC qTrace} {$D+} {$ENDC}
  251. {$S MAFailureRes}
  252.  
  253. PROCEDURE ProgramBreak(grievance: Str255);
  254.  
  255.     BEGIN
  256.     ApplicationBeep;
  257.     {$IFC qDebug}
  258.     IF CanReadLn THEN
  259.         BEGIN
  260.         DebugForceOutput(forceOn, forceUnchanged);
  261.         WriteLn('ProgramBreak: ', grievance);
  262.         DebugEndForce;
  263.         EnterMacAppDebugger;
  264.         END
  265.     ELSE
  266.         DebugStr(grievance);
  267.     {$ELSEC}
  268.     DebugStr(grievance);
  269.     {$ENDC}
  270.     END;
  271. {$Pop}
  272.  
  273. {--------------------------------------------------------------------------------------------------}
  274. {$Push} {$IFC qTrace} {$D+} {$ENDC}
  275. {$S MAFailureRes}
  276.  
  277. PROCEDURE ProgramReport(grievance: Str255;
  278.                         break: Boolean);
  279.  
  280.     BEGIN
  281.     ApplicationBeep;
  282.     {$IFC qDebug}
  283.     IF CanReadLn THEN
  284.         BEGIN
  285.         DebugForceOutput(forceOn, forceUnchanged);
  286.         WriteLn('ProgramReport: ', grievance);
  287.         DebugEndForce;
  288.         IF break THEN
  289.             EnterMacAppDebugger;
  290.         END
  291.     ELSE
  292.         DebugStr(grievance);
  293.     {$ELSEC}
  294.     DebugStr(grievance);
  295.     {$ENDC}
  296.     END;
  297. {$Pop}
  298.  
  299. {--------------------------------------------------------------------------------------------------}
  300. {$S MAFailureRes}
  301.  
  302. FUNCTION HandlerExists(testFailInfoPtr: FailInfoPtr): Boolean;
  303.  
  304.     PROCEDURE DoToHandler(pf: FailInfoPtr);
  305.  
  306.         BEGIN
  307.         IF pf = testFailInfoPtr THEN
  308.             HandlerExists := true;
  309.         END;
  310.  
  311.     BEGIN
  312.     HandlerExists := false;
  313.     EachFailureHandlerDo(DoToHandler);
  314.     END;
  315.  
  316. {--------------------------------------------------------------------------------------------------}
  317. {$S MAFailureRes}
  318.  
  319. PROCEDURE Success(VAR fi: FailInfo);
  320.  
  321.     BEGIN
  322.     {$IFC qDebug}
  323.     IF gTopHandler <> @fi THEN
  324.         BEGIN
  325.         Write('gTopHandler: ');
  326.         WritePtr(gTopHandler);
  327.         Write(', parameter: ');
  328.         WritePtr(@fi);
  329.         WriteLn;
  330.         Write('Problem with Success: ');
  331.         IF HandlerExists(@fi) THEN
  332.             Write('too few ')
  333.         ELSE
  334.             Write('too many ');
  335.         Write('calls to Success');
  336.         WriteLn;
  337.         ProgramBreak('');
  338.         END;
  339.     {$EndC}
  340.  
  341.     gTopHandler := fi.nextInfo;
  342.     END;
  343.